package com.example.Lezedata.ui.activity.history;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.LayoutInflaterFactory;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.Lezedata.R;
import com.example.Lezedata.dataModel.GoodsProjectModel;
import com.example.Lezedata.net.GBError;
import com.example.Lezedata.net.GBNetRequest;
import com.example.Lezedata.net.ServerResponseObserver;
import com.example.Lezedata.ui.activity.baseClass.GBBaseVc;
import com.example.Lezedata.ui.activity.record.MemberRecordVC;
import com.example.Lezedata.util.GBActivityManager;
import com.example.Lezedata.util.GBTimeManager;
import com.example.Lezedata.util.GBUtil;

import org.w3c.dom.Text;

import java.util.ArrayList;

import bid.Bid;
import common.Common;

class HistoryVcCell extends FrameLayout{

    TextView roundView;
    TextView raffledView;
    ImageView head;
    TextView winner;
    TextView code;
    TextView bids;

    private Bid.GoodsWinPrizeListResponse.GoodsWinPrizeItem goodsWinPrizeItem;

    public static HistoryVcCell newInstance(Context context) {
        HistoryVcCell fragment = (HistoryVcCell) LayoutInflater.from(context).inflate(R.layout.history_vc_cell,null,false);
        return fragment;
    }
    public HistoryVcCell(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        findView();
        addAction();
    }

    void addAction(){
        head.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                GBBaseVc context = GBActivityManager.sharedManager().currentVc;
                Intent intent = new Intent(context, MemberRecordVC.class);
                intent.putExtra("userId","123");
                context.jumpActivityIntent(intent);
            }
        });
    }

    void findView(){
         roundView = findViewWithTag("roundView");
         raffledView = findViewWithTag("raffledView");
         head = findViewWithTag("head");
         winner = findViewWithTag("winner");
         code = findViewWithTag("code");
         bids = findViewWithTag("bids");
    }

    public void setGoodsWinPrizeItem(Bid.GoodsWinPrizeListResponse.GoodsWinPrizeItem goodsWinPrizeItem) {
        this.goodsWinPrizeItem = goodsWinPrizeItem;
        GBUtil.setHeadImage(head,goodsWinPrizeItem.getImgUrl());
        roundView.setText(GBUtil.RoundToString(goodsWinPrizeItem.getBidRound()));
        raffledView.setText(String.format("Raffled: %s", GBTimeManager.timesYMD_HMS(goodsWinPrizeItem.getCreateTime())));
        winner.setText(String.format("Winner: %s",GBUtil.mobileReplaceToXxx(goodsWinPrizeItem.getPhone())));


        String bStr = String.format("%s bids",goodsWinPrizeItem.getBidCount());
        String allS = String.format("Bought:%s",bStr);
        int index = allS.indexOf(bStr);
        SpannableString spannableString = GBUtil.textAddColor(allS,getResources().getColor(R.color.app_color_main),index,index+bStr.length());
        bids.setText(spannableString);

        String bStr1 = String.format("%s",goodsWinPrizeItem.getBidCode());
        String allS1 = String.format("Wish Code:%s",bStr1);
        int index1 = allS1.indexOf(bStr1);
        SpannableString spannableString1 = GBUtil.textAddColor(allS1,getResources().getColor(R.color.app_color_yellow),index1,index1+bStr1.length());
        code.setText(spannableString1);
    }
}

public class HistoryVc extends GBBaseVc {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_histroy_vc;
    }

    @Override
    public CharSequence title() {
        return "History";
    }

    ListView listView;
    ArrayList<Bid.GoodsWinPrizeListResponse.GoodsWinPrizeItem> models = new ArrayList();
    private int pageStart = 0;
    private int pageSize = 20;

    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        listView = findViewByTag("listView");
        addLineToHeaderFooter();
        setAdapter();
        loadData();
    }

    void addLineToHeaderFooter(){
        TextView line = new TextView(mContext);
        line.setMaxHeight(GBUtil.pxTo2dp(mContext,15));
        line.setBackground(new ColorDrawable(Color.TRANSPARENT));
        listView.addHeaderView(line);

        TextView line1 = new TextView(mContext);
        line1.setMaxHeight(GBUtil.pxTo2dp(mContext,15));
        line1.setBackground(new ColorDrawable(Color.TRANSPARENT));
        listView.addFooterView(line1);
    }

    void loadData(){
        final Common.PageInfo pageInfo = Common.PageInfo.newBuilder().setPageStart(pageStart).setPageSize(pageSize).build();
        GBNetRequest.stub().goodsWinPrizeList(Bid.IdPageRequest.newBuilder().setId(getBid()).setPageInfo(pageInfo).build(),
                ServerResponseObserver.observer(new ServerResponseObserver.requestListener<bid.Bid.GoodsWinPrizeListResponse>() {
                    @Override
                    public void complete(bid.Bid.GoodsWinPrizeListResponse value, GBError error) {
                        if (error == null){
                            if (pageInfo.getPageStart() == 0){
                                models.clear();
                            }
                            models.addAll(value.getListList());
                            adapter.notifyDataSetChanged();
                        }
                    }
                })
        );
    }

    void setAdapter() {
        listView.setAdapter(adapter);
    }

    BaseAdapter adapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return models.size();
        }

        @Override
        public Bid.GoodsWinPrizeListResponse.GoodsWinPrizeItem getItem(int position) {
            return models.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null){
                convertView = HistoryVcCell.newInstance(mContext);
            }
            HistoryVcCell cell = (HistoryVcCell)convertView;
            cell.setGoodsWinPrizeItem(getItem(position));
            return convertView;
        }
    };


    String getBid(){
        String bid = getIntent().getStringExtra("goodsId");
        if (bid == null){
            bid = "";
        }
        return bid;
    }
}